﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class controls_ctrlPager : System.Web.UI.UserControl
{
    #region CONSTANTS

    public const int SHOW_PAGES_COUNT = 9;
    public const string LINK_PAGER_NAME = "lnkPage";

    private const string PAGE_NUMBER_KEY = "PageNumber";
    private const string LAST_COUNT_KEY = "LastItemCount";
    private const string PAGE_SIZE_KEY = "ctrlPager_PageSize";

    #endregion CONSTANTS

    #region VARIABLES

    private bool mustRender;
    private bool rendered;
    private int itemCount = -1;
    private int size = -1;
    private int pageNumber = -1;

    public event GridViewPageEventHandler PageIndexChanged;

    #endregion VARIABLES

    #region PROPERTIES

    public int ItemCount
    {
        get
        {
            if (itemCount < 0)
            {
                object obj = ViewState[LAST_COUNT_KEY];
                if (obj != null)
                    itemCount = (int)obj;
                else
                    itemCount = 0;
            }
            return itemCount;
        }
        set
        {
            mustRender = itemCount != value || mustRender;
            itemCount = value;
            ViewState[LAST_COUNT_KEY] = itemCount;
        }
    }

    public int PageSize
    {
        get
        {
            if (size < 0)
            {
                object obj = ViewState[PAGE_SIZE_KEY];
                if (obj != null)
                    size = (int)obj;
                else
                    size = 50;
            }
            return size;
        }
        set
        {
            mustRender = size != value || mustRender;
            size = value;
            ViewState[PAGE_SIZE_KEY] = size;
        }
    }

    public int PageNumber
    {
        get
        {
            if (pageNumber < 1)
            {
                object obj = ViewState[PAGE_NUMBER_KEY];
                if (obj != null && (int)obj > 0)
                    pageNumber = (int)obj;
                else
                    pageNumber = 1;
            }
              
            return pageNumber;
        }
        set
        {
            mustRender = pageNumber != value || mustRender;
            pageNumber = value;
            ViewState[PAGE_NUMBER_KEY] = pageNumber;
        }
    }

    #endregion PROPERTIES

    #region METHODS

    protected override void EnsureChildControls()
    {
        base.EnsureChildControls();
        if (!rendered || mustRender)
            RenderPager();
    }

    public void RenderPager()
    {
        int visiblePageCount = SHOW_PAGES_COUNT;
        int lastIndex = 1;

        LinkButton lnk = null;

        if (ItemCount == 0)
            lastIndex = 1;
        else if (ItemCount % PageSize == 0)
            lastIndex = ItemCount / PageSize;
        else
            lastIndex = ItemCount / PageSize + 1;

        int oneSidePage = (visiblePageCount - 1) / 2;
        int startIndex = PageNumber - oneSidePage;
        int distance = 0;

        if (startIndex < 1)
        {
            distance = visiblePageCount - lastIndex;
            if (distance > 0)
                visiblePageCount -= distance;
            startIndex = 1;
        }
        else if (PageNumber + oneSidePage > lastIndex)
        {
            startIndex = lastIndex - visiblePageCount + 1;
            if (startIndex < 1)
            {
                distance = visiblePageCount - lastIndex;
                if (distance > 0)
                    visiblePageCount -= distance;
                startIndex = 1;
            }
        }

        pnlPager.Controls.Clear();

        lnk = new LinkButton();
        lnk.ID = LINK_PAGER_NAME + "_first";
        lnk.CommandArgument = "1";
        lnk.CommandName = "Page";
        lnk.Text = "<<";
        lnk.Command += new CommandEventHandler(lnkPage_Command);
        lnk.CssClass = "cmsPager-link";

        pnlPager.Controls.Add(lnk);

        for (int i = 0; i < visiblePageCount; i++)
        {
            lnk = new LinkButton();
            lnk.ID = LINK_PAGER_NAME + i.ToString();
            lnk.CommandArgument = (startIndex + i).ToString();
            lnk.CommandName = "Page";
            lnk.Text = (startIndex + i).ToString();
            lnk.Command += new CommandEventHandler(lnkPage_Command);
            lnk.CssClass = "cmsPager-link";

            if (startIndex + i == PageNumber)
                lnk.Style[HtmlTextWriterStyle.FontWeight] = "Bold";

            pnlPager.Controls.Add(lnk);
        }

        lnk = new LinkButton();
        lnk.ID = LINK_PAGER_NAME + "_last";
        lnk.CommandArgument = lastIndex.ToString();
        lnk.CommandName = "Page";
        lnk.Text = ">>";
        lnk.Command += new CommandEventHandler(lnkPage_Command);
        lnk.CssClass = "cmsPager-link";
        pnlPager.Controls.Add(lnk);

        rendered = true;
        mustRender = false;
    }

    #endregion METHODS

    #region EVENTS

    void lnkPage_Command(object sender, CommandEventArgs e)
    {
        PageNumber = int.Parse(e.CommandArgument.ToString());
        if (PageIndexChanged != null)
            PageIndexChanged(sender, new GridViewPageEventArgs(pageNumber));
    }

    #endregion EVENTS

    #region PAGE EVENTS

    protected void Page_Init(object sender, EventArgs e)
    {
        mustRender = false;
        rendered = false;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            RenderPager();
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        pnlPager.Visible = ItemCount > 0;
    }

    #endregion PAGE EVENTS
}
